layui.use(['layer', 'form', 'laytpl', 'util', 'element'], function () {
    var $ = layui.$;
    var layer = layui.layer;
//关闭
    $(document).on('click', '.close', function () {
        $('.tan').hide();
    });
//触焦变色
    $(document).on('focus', 'input', function () {
        $(this).removeClass('red');
    });
//失去焦点验证当前表单

    $(document).on('blur', '#name2', function () {
        var errtxt = "",
            name = $('#name').val();
        if (name == "") {
            errtxt = "用户名不能为空";
            $('#name').addClass('red');
        } else {
            errtxt = "";
            $('#name').removeClass('red');
        }
        //错误提示消息，有错误则显示，没错误就隐藏
        showmsg(errtxt);
    })
    $(document).on('blur', '#passw2', function () {
        var errtxt = "",
            passw = $('#passw').val();
        if (passw == "") {
            errtxt = "请输入密码";
            $('#passw').addClass('red');
        } else {
            errtxt = "";
            $('#passw').removeClass('red');
        }
        //错误提示消息，有错误则显示，没错误就隐藏
        showmsg(errtxt);
    })
//验证
    $(document).on('click', '#Submit', function () {
        console.log("Submit");
        var thisid = "",
            errtxt = "";
        $('input').removeClass('red');
        var username = $('input[name=name]').val();
        var password = $('input[name=password]').val();

        if (username == "") {
            errtxt = "用户名不能为空";
            thisid = "name";
        }
        if (password == "") {
            errtxt = "请输入密码";
            thisid = "passw";
        }
        //错误提示消息，有错误则显示，没错误就隐藏
        showmsg(errtxt);
        //错误边框变红
        thisid ? $('input').removeClass('red').parents('form').find('#' + thisid).focus().addClass('red')
            : $('#' + thisid).removeClass('red');
        if (errtxt == "") {
            $.ajax({
                url: "rest/user/login",
                type: "post",
                data: {
                    userName: username,
                    passWord: password
                },
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    if (data.code == '200') {
                        layer.msg("登录成功");
                        $('.tan').hide();
                    } else {
                        layer.msg(data.msg);
                    }
                }, complete: function () {
                }
            });
        }
    });

    $(document).on('click', '#logout', function () {
        $.ajax({
            url: "rest/user/logout",
            type: "post",
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (data.code == '200') {
                    layer.msg("退出成功");
                    $('.tan').hide();
                } else {
                    layer.msg(data.msg);
                }
            }, complete: function () {
            }
        });
    });

    function showmsg(errtxt) {
        errtxt ? $('.error').show().text(errtxt) : $('.error').hide();
    }

});
